<template>
  <div>
    <div><h1>添加收款金额</h1></div>
    <div class="order_pay">{{order_pay}}</div>
    <div class="pay_panel">
      <!-- 12宫格的价格按钮 -->
      <div>
        <button @click="btn_press(1)">1</button
        ><button @click="btn_press(2)">2</button
        ><button @click="btn_press(3)">3</button>
        <button @click="btn_press('reset')">重置</button>
      </div>
      <div>
        <button @click="btn_press(4)">4</button
        ><button @click="btn_press(5)">5</button
        ><button @click="btn_press(6)">6</button>
        <button @click="btn_press('x')">删除</button>
      </div>
      <div>
        <button @click="btn_press(7)">7</button
        ><button @click="btn_press(8)">8</button
        ><button @click="btn_press(9)">9</button>
        <button @click="btn_press('@')">@</button>
      </div>
      <div>
        <button @click="btn_press(0)">0</button
        ><button @click="btn_press('.')">.</button
        ><button @click="btn_press('00')">00</button>
        <button @click="btn_confirm()">确定</button>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "SetOrderView",
  components: {},
  data() {
    return {
      order_pay: "0",
    };
  },
  mounted() {},
  methods: {
    //价格按钮方法
    btn_press(txt) {
      if(txt=='@'){
        return;
      }
      //点击删除按钮时的删除最后一位
      if (txt == "x") {
        //删除减去最后一位
        if (this.order_pay.length <= 1) {
          this.order_pay = 0;
          return;
        } else {
          this.order_pay = this.order_pay.substring(
            0,
            this.order_pay.length - 1
          );
          return;
        }
      }
      //重置
      if (txt == "reset") {
        this.order_pay = 0;
        return;
      }
      //设置最大长度不超过6
      if (this.order_pay.length > 6) {
        return;
      }
      //输入小数点
      if (txt == ".") {
        //开头为0的时候
        if (this.order_pay == "0") {
          this.order_pay += txt;
          return;
        } else {
          //否则直接加，但是这里前提是其中价格不包括小数点
          if (this.order_pay.includes('.')) {
            console.log(this.order_pay.includes('.'));
            return;
          } else {
            this.order_pay += txt;
            return;
          }
        }
      }
      //如果输入的是数字
      if (!isNaN(parseInt(txt))) {
        console.log("数字");
        //开头等于0的情况
        if (this.order_pay == "0") {
            this.order_pay=""+txt;
        } else {
          this.order_pay += txt;
          //保留小数点后两位
        //   if(this.order_pay.includes('.')){
        //     this.order_pay = this.order_pay.substring(this.order_pay.indexOf('.'),this.order_pay.indexOf('.')+2)
        //   }
        }
      }
    },
    //确定按钮
    btn_confirm(){
      //点击确定，将当前输入的价格，传入到下一个页面，扫码页面
      //如果当前价格是0
      if(this.order_pay=='0'){
        alert("请输入价格");
        return;
      }else{
        localStorage.setItem('order_pay', this.order_pay);
        this.$router.push('/scancode');
      }
    }
  },
};
</script>
<style scoped>
.order_pay {
  font-size: 48px;
  font-weight: 800;
}

.pay_panel button {
  width: 50px;
  height: 50px;
}
</style>